package svc

import (
	"github.com/tal-tech/go-zero/core/stores/cache"
	"github.com/tal-tech/go-zero/core/stores/sqlx"
	"github.com/tal-tech/go-zero/core/syncx"
	"monitor/shared"
	"monitor/user/api/internal/config"
	"monitor/user/model"
)

type ServiceContext struct {
	Config      config.Config
	UserModel   model.TbUserModel
	StatusModel model.TbStatusModel
	AlarmModel  model.TbAlarmModel
	StatModel   model.TbStatModel
	Cache       cache.Cache
}

func NewServiceContext(c config.Config) *ServiceContext {
	conn := sqlx.NewMysql(c.Mysql.DataSource)
	// redis cache 模式
	um := model.NewTbUserModel(conn, c.CacheRedis)
	st := model.NewTbStatusModel(conn, c.CacheRedis)
	al := model.NewTbAlarmModel(conn, c.CacheRedis)
	sta := model.NewTbStatModel(conn, c.CacheRedis)
	ca := cache.New(c.CacheRedis, syncx.NewSharedCalls(), cache.NewStat("cmdset"), shared.ErrNotFound)
	return &ServiceContext{
		Config:      c,
		UserModel:   um,
		StatusModel: st,
		AlarmModel:  al,
		StatModel:   sta,
		Cache:       ca,
	}
}
